package com.plugin.core.util;


import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author Mark by 2023/02/06
 */
@Slf4j
public class MyBase64Util {


    public static final String UTF_8 = String.valueOf(StandardCharsets.UTF_8);
    public static Base64.Encoder encoder;
    //即为安全的编码方式，替换“+” “/” “-”为“_”
    public static Base64.Encoder urlEncoder;
    public static Base64.Decoder decoder;
    public static Base64.Decoder urlDecoder;


    static {
        encoder = Base64.getEncoder();
        urlEncoder = Base64.getUrlEncoder();
        decoder = Base64.getMimeDecoder();
        urlDecoder = Base64.getUrlDecoder();
    }

    /**
     * 字节转字节-加密
     */
    public static byte[] encode(byte[] bytes) {
        return encoder.encode(bytes);
    }

    /**
     * 字符串转字节-加密
     */
    public static byte[] encode2Byte(String string) {
        return encode(string.getBytes());
    }
    /**
     * 字节转字符串-加密
     */
    public static String encode2String(byte[] bytes) {
        return encoder.encodeToString(bytes);
    }

    /**
     * 字符串转字符串-加密
     */
    public static String encode(String s) {
        byte[] encode = encode(s.getBytes());
        try {
            return new String(encode, UTF_8);
        } catch (UnsupportedEncodingException e) {
            log.error("exception:",e);
        }
        return null;
    }


    /**
     * 字节转字节-解密
     */
    public static byte[] decode(byte[] bytes) {
        return decoder.decode(bytes);
    }

    /**
     * 字符串转字节-解密
     */
    public static byte[] decode2Byte(String s) {
        return decoder.decode(s.getBytes());
    }

    /**
     * 字节转字符串-解密
     */
    public static String decode2String(byte[] bytes) {
        try {
            return new String(decoder.decode(bytes),UTF_8);
        } catch (UnsupportedEncodingException e) {
            log.error("exception:", e);
        }
        return null;
    }

    /**
     * 字符串转字符串-解密
     */
    public static String decode(String s) {
        byte[] decode;
        try {
            if(s != null){
                decode = decoder.decode(s);
                return new String(decode, UTF_8);
            }
        } catch (UnsupportedEncodingException e) {
            log.error("exception:", e);
        }
        return null;
    }

    /**
     * 字节转字节-网络加密
     */
    public static byte[] urlEncode(byte[] bytes) {
        return urlEncoder.encode(bytes);
    }

    /**
     * 字符串转字符串-网络加密
     */
    public static String urlEncode(String string) {
        byte[] encode = urlEncode(string.getBytes());
        try {
            return new String(encode, UTF_8);
        } catch (UnsupportedEncodingException e) {
            log.error("exception:", e);
        }
        return null;
    }

    /**
     * 字节转字符串-网络加密
     */
    public static String urlEncode2String(byte[] bytes) {
        return urlEncoder.encodeToString(bytes);
    }

    /**
     * 字符串转字节-网络加密
     */
    public static byte[] urlEncode2Byte(String string) {
        return urlEncode(string.getBytes());
    }

    /**
     * 字节转字节-网络解密
     */
    public static byte[] urlDecode(byte[] bytes) {
        return urlDecoder.decode(bytes);
    }

    /**
     * 字符串转字节-网络解密
     */
    public static byte[] urlDecode2Byte(String string) {
        return urlDecode(string.getBytes());
    }

    /**
     * 字节转字符串-网络解密
     */
    public static String urlDecode2String(byte[] bytes) {
        try {
            return new String(urlDecode(bytes),UTF_8);
        } catch (UnsupportedEncodingException e) {
            log.error("exception:", e);
        }
        return null;
    }

    /**
     * 字符串转字符串-网络解密
     */
    public static String urlDecode(String string) {
        byte[] decode = urlDecode(string.getBytes());
        try {
            return new String(decode, UTF_8);
        } catch (UnsupportedEncodingException e) {
            log.error("exception:", e);
        }
        return null;
    }

}
